package xna.android;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;

public class Sprite {
	
	private static final String TAG = Sprite.class.getSimpleName();

	private Bitmap bitmap;		// imagem
	private Vector2 position; // x,y
	private boolean touched;	// para o toque
	private Speed speed;
	private boolean visible;
	private Paint p;
	private Rect intersectionRect = new Rect(1,1,1,1);
	
	
	
	public Sprite(Bitmap bitmap, Vector2 position)
	{
		this.bitmap = bitmap;
		this.position = position;
		speed = new Speed(2.0f, 2.0f);
		visible = true;
		
		p = new Paint();
		p.setColor(Color.BLUE); 
	}

	public Sprite(Bitmap bitmap, int x, int y) {
		this.bitmap = bitmap;
		this.position.x = x;
		this.position.y = y;
		speed = new Speed(2.0f, 2.0f);
		visible = true;	
	}
	
	public int getX()
	{
		return (int)this.position.x;
	}
	
	public void setX(int x)
	{
		this.position.x = x;
	}
	
	public int getY()
	{
		return (int)this.position.y;
	}
	
	public void setY(int y)
	{
		this.position.y = y;
	}

	public Bitmap getBitmap()
	{
		return bitmap;
	}
	
	public void setBitmap(Bitmap bitmap)
	{
		this.bitmap = bitmap;
	}
	public Vector2 getPosition()
	{
		return this.position;
	}
	public void setPosition(Vector2 pos)
	{
		this.position = pos;
	}
	
	//para o toque
	
	public boolean isTouched()
	{
		return touched;
	}

	public void setTouched(boolean touched)
	{
		this.touched = touched;
	}

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	public Speed getSpeed() {
		return speed;
	}

	public void setSpeed(Speed speed) {
		this.speed = speed;
	}
	public Rect getRect()
	{
		return new Rect((int)this.getPosition().x, (int)this.getPosition().y, (int)this.getPosition().x+ this.getBitmap().getWidth(), (int)this.getPosition().y + this.getBitmap().getHeight());
	}
	public void handleActionDown(int eventX, int eventY)
	{
		
	}
	
	//para o desenho
		
		public boolean contains (Vector2 point)
		{
			Rect myRect = this.getRect();
			if (point.x > myRect.left && point.x < myRect.right && point.y > myRect.top && point.y < myRect.bottom)
				return true;
			return false;
		}
		public boolean contains(Sprite sprite)
		{
			Rect myRect = this.getRect();
			Rect other = sprite.getRect();
			
			if (other.left >= myRect.left && other.right < myRect.right && other.top > myRect.top && other.bottom <= myRect.bottom)
				return true;
			
			return false;
		}
		public boolean intersects(Sprite sprite)

		{
			Rect myRect = this.getRect();
			Rect other = sprite.getRect();
			if (myRect.right < other.left ||  myRect.left > other.right || myRect.bottom < other.top || myRect.top > other.bottom)
				return false;
			return true;
		}
		// colisao pixel a pixel
		//
		public boolean isCollide(Sprite other)
		{
			if (other.getRect().intersect(this.getRect())){
				
				int initX = Math.max(this.getRect().left,other.getRect().left);
				int endX = Math.min(this.getRect().right,other.getRect().right);
				
				int initY = Math.max(this.getRect().top, other.getRect().top);
				int endY = Math.min(this.getRect().bottom, other.getRect().bottom);
				
				intersectionRect = new Rect(initX,initY,endX,endY);
				
				for (int x = initX; x < endX; x++ )
				{
					for (int y = initY; y < endY; y++)
					{
						int x1 = (int)(x - this.position.x);
						int y1 = (int)(y - this.position.y);
						int x2 = (int)(x - other.position.x);
						int y2 = (int)(y - other.position.y);
						
						int color2 = other.getBitmap().getPixel(x2, y2);
						int color1 = this.getBitmap().getPixel(x1, y1);
						
						Log.d("SPRITE","Color: " + (color2 >> 8) + " " + Color.GREEN );
						if ( color2 != Color.TRANSPARENT && color1 != Color.TRANSPARENT)
							return true;
					}
				}
			}
			return false;
		}
		
		public boolean intersects(int x, int y, int width, int height)
		{
			if(
					(this.position.x+this.bitmap.getWidth()<x)//fora pela esquerda
					||
					(this.position.x>x+width)//fora pela direita
					||
					(this.position.y+this.bitmap.getHeight()<y)//fora por cima
					||
					(this.position.y>y+height)//fora por baixo
					)
					{
						return false;
					}
					else
					{
						return true;
					}
		}
		
		public void update(int windowWidth, int windowHeight)
		{
			/*x+=speed.getXv()*speed.getxDirection();
			y+=speed.getYv()*speed.getyDirection();
			
			if(x<0)
			{
				//Log.d(TAG, "bati no canto esquerdo");
				speed.toggleXDirection();
			}
			
			if(x>windowWidth)
			{
				//Log.d(TAG, "bati no canto direita");
				speed.toggleXDirection();
			}
			
			if(y<0)
			{
				//Log.d(TAG, "bati no canto esquerdo");
				speed.toggleYDirection();
			}
			
			if(y>windowHeight)
			{
				//Log.d(TAG, "bati no canto esquerdo");
				speed.toggleYDirection();
			}*/
		}	
		public void draw(Canvas canvas)
		{
			if(visible){
				canvas.drawRect(intersectionRect, p);
				
				canvas.drawBitmap(bitmap, position.x, position.y, null);
			}
			
		}
}